package com.zqweb.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.zqweb.common.valid.AddGroup;
import com.zqweb.common.valid.UpdateForStatusGroup;
import com.zqweb.common.valid.UpdateGroup;
import com.zqweb.gulimall.product.vo.BrandVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.zqweb.gulimall.product.entity.BrandEntity;
import com.zqweb.gulimall.product.service.BrandService;
import com.zqweb.common.utils.PageUtils;
import com.zqweb.common.utils.R;



/**
 * 品牌
 *
 * @author zq
 * @email zq@gmail.com
 * @date 2021-11-26 18:38:54
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查询brandIds集合对应的结果集合
     */
    @GetMapping("/info/brandIds")
    public R brandIdsInfo(@RequestParam List<Long> brandIds){
        List<BrandEntity> brands = brandService.brandIdsInfo(brandIds);
        // 由于此接口对于brand的信息仅有brandId、brandName两项，其余内容无需传输，可在此将BrandEntity改成BrandVo封装
        List<BrandVo> brandVos = null;
        if(brands != null && brands.size() > 0){
            brandVos = brands.stream().map(brand -> {
                BrandVo brandVo = new BrandVo();
                brandVo.setBrandId(brand.getBrandId());
                brandVo.setBrandName(brand.getName());
                return brandVo;
            }).collect(Collectors.toList());
        }

        return R.ok().put("brands",brandVos);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@Validated(value = {AddGroup.class}) @RequestBody BrandEntity brand/*, BindingResult result */){
        // 正常设计业务代码，异常处理交给@RestControllerAdvice进行切面监控通知来完成

        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@Validated(value = {UpdateGroup.class}) @RequestBody BrandEntity brand){
//		brandService.updateById(brand);
        brandService.updateDetail(brand);
        return R.ok();
    }
    /**
     * 仅修改status字段，没有提供其他的品牌信息
     */
    @RequestMapping("/update/status")
    public R updateForStatus(@Validated(value = {UpdateForStatusGroup.class}) @RequestBody BrandEntity brand){
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
